﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CDB.LinqModel.Data;
using CDB.LinqData;
using CDB.LinqModel;
using System.Configuration;

public partial class Register : System.Web.UI.Page
{

    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    User user;
    Country country;
    State state;
    List<State> states = new List<State>();
    List<Country> countries = new List<Country>();
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindCountries();
            BindStates(1); //Note: By DEfault we load US States.
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        CDB.LinqModel.User nUser = new CDB.LinqModel.User(_dataObject);
        nUser.UserName = txtUsername.Text;
        nUser.Email = txtEmail.Text;
        nUser.GetUserByUserName();

        if (nUser.Id != 0)
        {
            // Exists.
            dvMessage.InnerHtml = "<font color='red'>User with the same username or email address already exists.</font>";
        }
        else
        {
            dvMessage.InnerText = string.Empty;
            user = new User(_dataObject);
            user.FirstName = txtFirstName.Text;
            user.LastName = txtLastName.Text;
            user.Email = txtEmail.Text;
            user.Phone = txtPhone.Text;
            user.Address1 = txtAddress1.Text;
            user.Address2 = txtAddress2.Text;
            user.Country = Convert.ToInt32(drpCountry.SelectedItem.Value);
            user.State = Convert.ToInt32(drpState.SelectedItem.Value);
            user.City = txtCity.Text;
            user.Zip = txtZip.Text;
            user.PropertyLocation = drpPropertyLocation.SelectedItem.Value;
            user.ServiceRequired = drpServiceRequired.SelectedItem.Value; 
            user.UserName = txtUsername.Text;
            user.Password = txtPassword.Text;
            user.IsActive = false;
            user.UniqueId = Guid.NewGuid();

            user.Create();
            string mailBody = string.Empty;
            mailBody = "Thank you for your Interest in CDB and Our Services. Please click <a href='http://cdb.hexatechnolabs.com/Activation.aspx?Id=" + user.UniqueId + "'>here</a> to activate your account.";

            CDB.LinqModel.CDBGeneral.sendEmailToUsers(txtEmail.Text, "CDB New Registeration", mailBody, string.Empty);

            dvMessage.InnerHtml = "<font color='red'>Thank you for your Interest in CDB and Our Services.</font>";
            clearControls();
        }
    }
    private void clearControls()
    {
        txtFirstName.Text = string.Empty;
        txtLastName.Text = string.Empty;
        txtEmail.Text = string.Empty;
        txtPhone.Text = string.Empty;
        txtAddress1.Text = string.Empty;
        txtAddress2.Text = string.Empty;
        drpCountry.SelectedIndex = -1;
        drpState.SelectedIndex = -1;
        txtCity.Text = string.Empty;
        txtZip.Text = string.Empty;
        drpPropertyLocation.SelectedIndex = -1;
        drpServiceRequired.SelectedIndex = -1;
        txtUsername.Text = string.Empty;
        txtPassword.Text = string.Empty;
        txtConfirmPassword.Text = string.Empty;
    }
    private void BindCountries()
    {
        country = new Country(_dataObject);
        countries = country.GetAllCountries();

        drpCountry.DataSource = countries;
        drpCountry.DataValueField = "Id";
        drpCountry.DataTextField = "Name";
        drpCountry.DataBind();
    }
    private void BindStates(int countryId)
    {
        state = new State(_dataObject);
        states = state.GetStatesByCountryId(countryId);

        drpState.DataSource = states;
        drpState.DataValueField = "Id";
        drpState.DataTextField = "Name";
        drpState.DataBind();
    }
}